﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YJM.Database.Models;

namespace YJM.Database
{
    public class DbContext
    {
        public SqlSugarClient Db;   //用来处理事务多表查询和复杂的操作

        //public static SqlSugarClient Current
        //{
        //    get
        //    {
        //        return new SqlSugarClient(new ConnectionConfig()
        //        {
        //            ConnectionString = AppSettings.Configuration["DbConnection:ConnectionString"],
        //            DbType = (DbType)Convert.ToInt32(AppSettings.Configuration["DbConnection:DbType"]),
        //            IsAutoCloseConnection = false,
        //            IsShardSameThread = true,
        //            InitKeyType = InitKeyType.Attribute,
        //            ConfigureExternalServices = new ConfigureExternalServices()
        //            {
        //                DataInfoCacheService = new RedisCache()
        //            },
        //            MoreSettings = new ConnMoreSettings()
        //            {
        //                IsAutoRemoveDataCache = true
        //            }
        //        });
        //    }
        //}

        public DbContext()
        {
            string jsonfile = "appsettings.json";//JSON文件路径

            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    Db = new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = o["DbConnection"]["ConnectionString"].ToString(),
                        DbType = (DbType)Convert.ToInt32(o["DbConnection"]["DbType"].ToString()),
                        IsAutoCloseConnection = true,
                        //IsShardSameThread = true,
                        InitKeyType = InitKeyType.Attribute,
                        //ConfigureExternalServices = new ConfigureExternalServices()
                        //{
                        //    DataInfoCacheService = new RedisCache()
                        //},
                        MoreSettings = new ConnMoreSettings()
                        {
                            IsAutoRemoveDataCache = true
                        }
                    });
                    //调式代码 用来打印SQL 
                    Db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        Debug.WriteLine(sql);
                    };
                }
            }

            ////生成表格
            //Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Base_Equipment));//这样一个表就能成功创建了
            //Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Base_Factory));//这样一个表就能成功创建了
            //Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Base_MeasurePoint));//这样一个表就能成功创建了
            //Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Base_Daq));//这样一个表就能成功创建了
            //Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Base_Channel));//这样一个表就能成功创建了
        }

        public DbSet<T> DbTable<T>() where T : class, new()
        {
            return new DbSet<T>(Db);
        }

        //public DbSet<Base_Company> BaseCompanyDb => new DbSet<Base_Company>(Db);


    }

    /// <summary>
    /// 扩展ORM
    /// </summary>
    public class DbSet<T> : SimpleClient<T> where T : class, new()
    {
        public DbSet(SqlSugarClient context) : base(context)
        {

        }
    }
}
